package com.xlro.servlet;

import com.xlro.domain.User;
import com.xlro.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "SelfPassServlet",urlPatterns = "/alterPass")
public class SelfPassServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] passes = req.getParameterValues("pass");

        User user = (User) req.getSession().getAttribute("user");
        UserService userService = new UserService();

        String title;
        String result;

        if(user.getPassword().equals(passes[0])&& passes[1]!=null && passes[1]!=""){

            if(passes[1].equals(passes[2])){
                title = "提示！";
                result = userService.alterPass(user.getId(),passes[1]);
                req.getRequestDispatcher("./selfInfo.jsp?title="+title+"&message=" +result).forward(req,resp);
            }else {
                title = "错误！";
                result = "新密码不一致";
                req.getRequestDispatcher("./selfInfo.jsp?title="+title+"&message="+result).forward(req,resp);
            }
        }else {
            title = "错误！";
            result = "旧密码不对";
            req.getRequestDispatcher("./selfInfo.jsp?title="+title+"&message="+result).forward(req,resp);
        }
    }
}
